layui.use(['upload','element','table','form','laytpl'],function(){
		 var $ = layui.jquery
  		,upload = layui.upload
			,table = layui.table
			,layer = layui.layer
			,element =layui.element
			,form = layui.form
			,laytpl = layui.laytpl
			 //监听提交
		 var teacherInfo = document.getElementById("teacherInfo").innerHTML;
		 var userKey = JSON.parse(sessionStorage.getItem("userKey"));
			 console.log(userKey)
			 if(userKey) {
			 	//如果存在,则说明登录过,先比对过期时间
			 	var delayTime = userKey.delayTime
			 	var nowTime = new Date().getTime()
			 	if(nowTime < delayTime) {
			 		//登录未失效
			 		laytpl(teacherInfo).render(userKey, function(html) {
			 			$("#teacherInfoView").html(html)
			 			element.render()
			 		})
			 	}
			 }
			 

		 	
			
		table.on('tool(studentList)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
				  var data = obj.data; //获得当前行数据
				  var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
				  var tr = obj.tr; //获得当前行 tr 的DOM对象
				 
				  if(layEvent === 'detail'){ //查看
				    //do somehing
				  } else if(layEvent === 'del'){ //删除
				    layer.confirm('真的删除行么', function(index){
				      obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
//				      console.log(data.id)
				      layer.close(index);
//				      向服务端发送删除指令
				      $.ajax({
				      	url:"http://localhost:8888/student/deleteById?id="+data.id,
				      	type:"get",
				      	success:function(data){
				      		layer.msg("删除成功")
				      		location.reload()
				      		table.reload()
				      	}
				      })
				    });
				  } else if(layEvent === 'edit'){ //编辑
				    //do something
				   layer.open({
					      type: 2,
					      title: obj.data.title,
					      shadeClose: false,
					      shade: false,
					      maxmin: true, //开启最大化最小化按钮
					      area: ['800px', '640px'],
					      content: "editStudent.html?id="+data.id,
					      cancel: function(index, layero){ 
						  	
						    layer.close(index)							
						}    
			    	});
				    //同步更新缓存对应的值
				   
				  }
			});
      
	
			
			
			
			var studentList = table.render({
					elem:'#studentList',
					url:"http://localhost:8888/student/getAllStudent",
					cols:[[
                        {field:'studentNo',  title: '学号'}
				      ,{field:'name',  title: '名字', sort: true}
				      ,{field:'gradeInfo',  title: '年级', sort: true}
				      ,{field:'classInfo',  title: '班级', sort: true}
				      ,{field:'major',  title: '专业'}
				      ,{field:'sex',  title: '性别',templet:function(d){
				      	return d.sex == 0?'女':'男'
				      }}
				      ,{fixed:'right',width:80,align:'center',title:"操作",toolbar:'#bar'}
					]]
			});
			
			var newStudentList = table.render({
					elem:'#newStudentList',
					url:"http://localhost:8888/student/getUploadExcel",
					cols:[[
						  {field:'id', width:80, title: 'ID', sort: true}
				      ,{field:'studentNo',  title: '学号'}
				      ,{field:'name',  title: '名字', sort: true}
				      ,{field:'major',  title: '专业', sort: true}
				      ,{field:'gradeInfo',  title: '年级', sort: true}
				      ,{field:'classInfo',  title: '班级', sort: true}
				      ,{field:'sex',  title: '性别',templet:function(d){
				      	return d.sex == 0?'女':'男'
				      }}
//				      ,{fixed:'right',align:'center',toolbar:'#bar'}
					]]
			});
			
			upload.render({
		    elem: '#uploadExcel'
		    ,url: 'http://localhost:8888/student/uploadExcel/'
		    ,accept: 'file' //普通文件
		    ,done: function(res, index, upload){
//	      		var jsonS = JSON.stringify(res.data)
				location.reload()
	      		newStudentList.reload();
		    },error:function(){
		    		layer.msg("上传失败，检查Excel格式")
		    }
  });
	})